﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nuane.Posterous;

namespace Writerous
{
    public partial class frmOptions : Form
    {
        private PluginSettings m_settings;
        private PosterousSession m_session;
        private PosterousSite m_selectedSite;

        public frmOptions(PluginSettings settings)
        {
            InitializeComponent();
            m_settings = settings;
            LoadSettings();
        }

        private void LoadSettings()
        {
            txbxEmailAddress.Text = m_settings.EmailAddress;
            txbxPassword.Text = m_settings.Password;
            lblSiteName.Text = string.Format("Currently using site: {0} ({1})", m_settings.SiteName, m_settings.SiteId);
            chbxAutoPost.Checked = m_settings.AutoPost;
            chbxPostToBlog.Checked = m_settings.PostToBlog;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txbxEmailAddress.Text.Length == 0 ||
                txbxPassword.Text.Length == 0 ||
                ((cmbSites.Items.Count == 0 && m_settings.SiteId == -1)))
            {
                MessageBox.Show("All fields are required.", "Please enter all fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Save settings.
            m_settings.EmailAddress = txbxEmailAddress.Text;
            m_settings.Password = txbxPassword.Text;
            if (cmbSites.Items.Count > 0)
            {
                m_selectedSite = cmbSites.SelectedItem as PosterousSite;
                m_settings.SiteId = m_selectedSite.Id;
                m_settings.SiteName = m_selectedSite.Name;
            }            
            m_settings.AutoPost = chbxAutoPost.Checked;
            m_settings.PostToBlog = chbxPostToBlog.Checked;
            Close();
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            if(txbxEmailAddress.Text.Length == 0 ||
                txbxPassword.Text.Length == 0)
            {
                MessageBox.Show("All fields are required.", "Please enter all fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            m_session = new PosterousSession(txbxEmailAddress.Text, txbxPassword.Text);
            cmbSites.DataSource = m_session.GetSites();
            cmbSites.SelectedIndex = 0;
        }

        private void cmbSites_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lnkClearSettings_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to clear all your settings? This can't be undone.",
                                                  "Clear Settings",
                                                  MessageBoxButtons.YesNo,
                                                  MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                // Clear the UI
                txbxEmailAddress.Text = string.Empty;
                txbxPassword.Text = string.Empty;
                cmbSites.Items.Clear();

                // Clear the settings
                m_settings.EmailAddress = string.Empty;
                m_settings.Password = string.Empty;
                m_settings.SiteId = -1;
                m_settings.SiteName = string.Empty;
                m_settings.PostToBlog = false;
                LoadSettings();
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }

        private void frmOptions_Load(object sender, EventArgs e)
        {
            ToolTip postToBlog = new ToolTip();
            postToBlog.AutoPopDelay = 5000;
            postToBlog.InitialDelay = 1000;
            postToBlog.ReshowDelay = 500;
            postToBlog.SetToolTip(this.chbxPostToBlog, "Selecting this option will post the blog post to the \nblog you're using in Live Writer as well as to Posterous.");
        }
    }
}
